/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.tracer.core.util;

public class NumberUtils {
    static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String toLowerHex(long v) {
        char[] data = new char[16];
        writeHexLong(data, 0, v);
        return new String(data);
    }

    public static void writeHexLong(char[] data, int pos, long v) {
        writeHexByte(data, pos + 0, (byte) ((int) (v >>> 56 & 255L)));
        writeHexByte(data, pos + 2, (byte) ((int) (v >>> 48 & 255L)));
        writeHexByte(data, pos + 4, (byte) ((int) (v >>> 40 & 255L)));
        writeHexByte(data, pos + 6, (byte) ((int) (v >>> 32 & 255L)));
        writeHexByte(data, pos + 8, (byte) ((int) (v >>> 24 & 255L)));
        writeHexByte(data, pos + 10, (byte) ((int) (v >>> 16 & 255L)));
        writeHexByte(data, pos + 12, (byte) ((int) (v >>> 8 & 255L)));
        writeHexByte(data, pos + 14, (byte) ((int) (v & 255L)));
    }

    public static void writeHexByte(char[] data, int pos, byte b) {
        data[pos + 0] = HEX_DIGITS[b >> 4 & 15];
        data[pos + 1] = HEX_DIGITS[b & 15];
    }
}
